package org.sgx.gwteditors.testgallery.gallery;

import java.util.Map;

import org.sgx.gwteditors.client.editor.event.EditorValueChangeEvent;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.editor.props.PropertyHaverUtil;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.basetypes.Color;
import org.sgx.gwteditors.client.impl1.basetypes.InRangeDouble;
import org.sgx.gwteditors.client.impl1.basetypes.SelectionList;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor2;
import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * in this test we will show you how to edit your java beans
 * without the propertyHaver methods (these will be added by the framework using gwt derreferred binding. 
 * @author sg
 *
 */
public class BeanEditorTest2 implements Test {

	@Override
/*---begintestmaincode---*/
public void test(Panel parent) {
	new EditorFramework1().start(); //do not forget of initialize the gwteditors framework !
	
	VerticalPanel vp = new VerticalPanel();
	vp.setWidth("100%");
	parent.add(vp);	//make sure the editor parent (vp) is attached to the dom before using the editor
	
	PropertyHaverEditor2<Person1> ed = new PropertyHaverEditor2<Person1>();
	vp.add(ed.getWidget());
	
	Person1 p1 = GWT.create(Person1.class);
	p1.setName("seba"); 
	p1.setAge(28);
	p1.setSize(new InRangeDouble(0, 30, 12)); 
	p1.setMale(true);
	p1.setEyeColor(new Color(255,0,0));
	p1.setCategory(new SelectionList(
			new String[]{"defense", "attack", "creation", "energy"}, 
			new String[]{"defense"}));
	p1.setSize(new InRangeDouble(20, 80, 40)); 
	
	Person1 p2 = GWT.create(Person1.class);
	p2.setName("Graciela"); 
	p2.setAge(55);
	p1.setSize(new InRangeDouble(0, 40, 32)); 
	p2.setMale(true);
	p2.setEyeColor(new Color(255,240,0));
	p2.setCategory(new SelectionList(
			new String[]{"defense", "attack", "creation", "energy"}, 
			new String[]{"attack"}));		
	p1.setMother(p2);
	System.out.println("from test 1 "+p1.getMother()+" - "+p1.propertyGet("mother"));
	
	ed.load(p1);
	
	ed.addValueChangeListener(new ValueChangeListener<Person1>() {			
		@Override
		public void notifyValueChange(EditorValueChangeEvent<Person1> evt) {
			Person1 val = evt.getSrc().flush();
			if(val==null&&evt.getSrc().getErrors().size()>0) {
				System.out.println("Error: "+evt.getSrc().getErrors().get(0));
			}
			else {
				System.out.println("property value change detected: "+val);
			}
		}
	});	
}
	
//The bean we want to edit

public static abstract class Person1 implements EditableBean {		
	String name;
	Integer age;
	Boolean male;
	Color eyeColor, nullColor;
	SelectionList<String> category;
	Person1 mother;
	InRangeDouble size; 	
	
	public Person1(){}

	@Override
	public String toString() {
		return PropertyHaverUtil.print(this); 
	}	
	
	public String getName() {
		return name;
	}		
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Boolean getMale() {
		return male;
	}
	public void setMale(Boolean male) {
		this.male = male;
	}
	public Color getEyeColor() {
		return eyeColor;
	}
	public void setEyeColor(Color eyeColor) {
		this.eyeColor = eyeColor;
	}
	public SelectionList<String> getCategory() {
		return category;
	}
	public void setCategory(SelectionList<String> category) {
		this.category = category;
	}
	public Color getNullColor() {
		return nullColor;
	}
	public void setNullColor(Color nullColor) {
		this.nullColor = nullColor;
	}
	public InRangeDouble getSize() {
		return size;
	}
	public void setSize(InRangeDouble size) {
		this.size = size;
	}
	public Person1 getMother() {
		return mother;
	}
	public void setMother(Person1 mother) {
		this.mother = mother;
	}		
}

/*---endtestmaincode---*/
	
	//test stuff
	@Override
	public String getName() {
		return "bean 2";
	}
	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.BeanEditorTest2());
	}
	@Override
	public String[] getTags() {
		return new String[]{};
	}
	@Override
	public String getDescription() {
		return "show how to use PropertyHaverEditor2 for editing anidated beans";
	}
}
